<?php
namespace Index\Controller;

use Index\Model\Page;
use Think\Controller;
use Think\Log;

class IssueController extends Controller
{

    protected $section, $team, $dep, $issue,$issuetype;

    function _initialize()
    {
        session("username", "admin_jspider");
        $this->section = M("Worksection");
        $this->team = M("Team");
        $this->dep = M("Department");
        $this->issue = M("Issue");
        $this->issuetype = M("Issuetype");
    }

    public function _empty($viewname)
    {
        $user = array();
        $user['username'] = session("username");
        $this->assign($user);
        $this->display(":$viewname");
    }


    public function issule_add()
    {
        $sections = $this->section->where(array("status" => 1))->field("sec_id,sec_name,sec_note")->order(array("sec_id" => "asc"))->select();
        $deps = $this->dep->where(array("status" => 1))->field("dep_id,dep_name,dep_note")->order(array("dep_id" => "asc"))->select();
        $teams = $this->team->where(array("status" => 1))->field("team_id,team_name,team_note")->order(array("team_id" => "asc"))->select();
        $issuetypes = $this->issuetype->where(array("status"=>1))->field("issuetype_id,issuetype_name,issuetype_note")->order(array("issuetype_id"=>"asc"))->select();
        $this->assign("sections", $sections);
        $this->assign("deps", $deps);
        $this->assign("teams", $teams);
        $this->assign("issuetypes",$issuetypes);
        $this->display(":issule-add");
    }


    public function issue_edit()
    {
        $id = intval(I("get.id"));
        $Issue = M("Issue");
        $result = $Issue->where(array("id" => $id))->find();
        Log::write($result->html);
        if (!empty($result['html'])) {
            Log::write("********************");
            $result['html'] = htmlspecialchars_decode($result['html']);
            $result['html'] = str_replace("\n", "<br/>", $result['html']);
            F("html", dump($result, false));
        }
        $sections = $this->section->where(array("status" => 1))->field("sec_id,sec_name,sec_note")->order(array("sec_id" => "asc"))->select();
        $deps = $this->dep->where(array("status" => 1))->field("dep_id,dep_name,dep_note")->order(array("dep_id" => "asc"))->select();
        $teams = $this->team->where(array("status" => 1))->field("team_id,team_name,team_note")->order(array("team_id" => "asc"))->select();
        $issuetypes = $this->issuetype->where(array("status"=>1))->field("issuetype_id,issuetype_name,issuetype_note")->order(array("issuetype_id"=>"asc"))->select();
        if(array_key_exists("team_ids",$result)){
            $ar = explode(",",$result["team_ids"]);
            $arr = array();
            foreach($ar as $k => $v){
                $arr[$v] = $v;
            }
            $result["team_ids"] = $arr;
            if(count($arr) == count($teams)){
                $this->assign("checked_all",true);
            }
        }
        $this->assign("sections", $sections);
        $this->assign("deps", $deps);
        $this->assign("teams", $teams);
        $this->assign("issuetypes",$issuetypes);
        Log::write(dump($result, false));
        $this->assign("edit", 1);
        $this->assign("issue", $result);
        $this->display(":issule-add");
    }

    public function issue_post()
    {
        $update = false;
        if (IS_POST && IS_AJAX) {
            $post_body = $GLOBALS['HTTP_RAW_POST_DATA'];//file_get_contents("php://input");
            $json = (array)json_decode($post_body);
            foreach ($json as $key => $value) {
                $_POST[$key] = $value;
            }
            $_POST["time"] = strtotime(I("post.time"));
            $edit = I("post.id");
            if (!empty($edit)) {
                $update = true;
            }
            F("data3", $_POST);
            $issue = M("Issue");
            $issue->create();
            if($update){
                $issue->save();
            } else {
                $issue->add();
            }
            Log::write(dump($json, false));
            $this->ajaxReturn(sp_ajax_return(array(), "success", 0));
        } else {
            $this->ajaxReturn(sp_ajax_return(array(), "invalid request", 1));
        }
    }

    public function issue_list()
    {
        $Issue = M("Issue");
        $params = I("param.");
        if (empty($_GET['p'])) {
            $_GET['p'] = 0;
        }
        $wheres['i.time'] = array();
        $wheres['i.time'][] = array('gt', 0);
        if (!empty($params['begintime'])) {
            $begintime = strtotime($params['begintime']);
            $wheres['i.time'][] = array('egt', $begintime);
        }
        if (!empty($params['endtime'])) {
            $endtime = strtotime($params['endtime']);
            $wheres['i.time'][] = array('elt', $endtime);
        }
        if (intval($params['sec_id'], 0) > 0) {
            $wheres['i.sec_id'] = $params['sec_id'];
        }
        if (intval($params['dep_id'], 0) > 0) {
            $wheres['i.dep_id'] = $params['dep_id'];
        }
        if (intval($params['team_id'], 0) > 0) {
            $wheres['i.team_id|i.team_ids'] = array($params['team_id'],array(array('like','%,'.$params['team_id'].',%'), array('like',$params['team_id'].',%'), array('like','%,'.$params['team_id']),'or'),'_multi'=>true);
        }
        if (!empty($params['issueregex'])) {
            $wheres['i.text'] = array('like', '%' . $params["issueregex"] . '%');
        }
        $count = $Issue->alias("i")->field(array("i.id", "i.sec_id", "i.dep_id", "i.time", "i.text", "i.issuetype_id","w.sec_name", "d.dep_name", "t.team_name"))->join(array(" JOIN __WORKSECTION__ w ON w.sec_id = i.sec_id", " JOIN __DEPARTMENT__ d ON d.dep_id = i.dep_id",
            " JOIN __TEAM__ t ON t.team_id = i.team_id"))->where($wheres)->count();
        F("count", $count);
        $sections = $this->section->where(array("status" => 1))->field("sec_id,sec_name,sec_note")->order(array("sec_id" => "asc"))->select();
        $deps = $this->dep->where(array("status" => 1))->field("dep_id,dep_name,dep_note")->order(array("dep_id" => "asc"))->select();
        $teams = $this->team->where(array("status" => 1))->field("team_id,team_name,team_note")->order(array("team_id" => "asc"))->select();
        $list = $Issue->alias("i")->field(array("i.id", "i.sec_id", "i.dep_id", "i.time", "i.text","i.issuetype_id","w.sec_name", "d.dep_name", "t.team_name"))->join(array(" JOIN __WORKSECTION__ w ON w.sec_id = i.sec_id", " JOIN __DEPARTMENT__ d ON d.dep_id = i.dep_id",
            " JOIN __TEAM__ t ON t.team_id = i.team_id"))->where($wheres)->order(array('i.id' => 'desc'))->page($_GET['p'] . ',10')->select();
        $this->assign('issues', $list);
        $this->assign("sections", $sections);
        $this->assign("deps", $deps);
        $this->assign("teams", $teams);
        $Page = new Page($count, 10);
        //$Page = new Page($count, 2);
        $show = $Page->show();
        $this->assign("count", $count);
        Log::write($show);
        F("page", $show);
        F("list", $list);
        $this->assign('page', $show);
        $this->assign("params", $params);
        $this->display(":issule-list");
    }

    public function issue_del()
    {
        $id = intval(I("get.id"));
        $issue = M("Issue");
        $issue->where(array("id" => $id))->delete();
        $this->ajaxReturn(sp_ajax_return(array(), "删除成功", 0));
    }


    public function issueBathDel(){
        if (IS_POST && IS_AJAX) {
            $post_body = $GLOBALS['HTTP_RAW_POST_DATA'];//file_get_contents("php://input");
            $json = (array)json_decode($post_body);
            $ids = implode(",",$json);
            $this->issue->delete($ids);
            $this->ajaxReturn(sp_ajax_return(array(), "success", 0));

        } else {
            $this->ajaxReturn(sp_ajax_return(array(), "invalid request", 1));
        }
    }


}